package io.rong.imkit.dialog

import android.content.Context
import android.graphics.Paint
import android.util.Log
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.custom.base.entity.OkHttpBodyEntity
import com.custom.base.http.OkHttpFromBoy
import com.custom.base.http.OkHttpManager
import com.custom.base.http.SDOkHttpResoutCallBack
import com.youth.banner.Banner
import com.youth.banner.listener.OnPageChangeListener
import io.rong.imkit.API
import io.rong.imkit.R
import io.rong.imkit.adapter.PageBuyMemberAdapter
import io.rong.imkit.entity.MemberSubscribeEntity
import razerdp.basepopup.BasePopupWindow

class MemberBuyDialog(var ctx: Context,var currentItem:Int,var listener :ChangeMembershipListener) :  BasePopupWindow(ctx) {
    init {
        setContentView(R.layout.dialog_member_buy)
        initView()
    }

    private fun initView() {
        val dialogCycleBanner = findViewById<Banner<*, *>>(R.id.dialog_cycle_banner)
        val dialogPurchaseNow = findViewById<TextView>(R.id.dialog_purchase_now)
        val dialogCancel = findViewById<ImageView>(R.id.dialog_close)
        val quarterContainer = findViewById<ConstraintLayout>(R.id.quarter_container)
        val itemQuarterTitle = findViewById<TextView>(R.id.item_quarter_title)
        val itemQuarterNumber = findViewById<TextView>(R.id.item_quarter_number)
        val itemQuarterName = findViewById<TextView>(R.id.item_quarter_name)
        val itemQuarterDiscountedPrice = findViewById<TextView>(R.id.item_quarter_discounted_price)
        val itemQuarterOriginalPrice = findViewById<TextView>(R.id.item_quarter_original_price)
        val itemQuarterDiscount = findViewById<TextView>(R.id.item_quarter_discount)

        val monthlyContainer = findViewById<ConstraintLayout>(R.id.monthly_container)
        val itemMonthlyTitle = findViewById<TextView>(R.id.item_monthly_title)
        val itemMonthlyNumber = findViewById<TextView>(R.id.item_monthly_number)
        val itemMonthlyName = findViewById<TextView>(R.id.item_monthly_name)
        val itemMonthlyDiscountedPrice = findViewById<TextView>(R.id.item_monthly_discounted_price)
        val itemMonthlyOriginalPrice = findViewById<TextView>(R.id.item_monthly_original_price)
        val itemMonthlyDiscount = findViewById<TextView>(R.id.item_monthly_discount)

        val weeksContainer = findViewById<ConstraintLayout>(R.id.weeks_container)
        val itemWeeksNumber = findViewById<TextView>(R.id.item_weeks_number)
        val itemWeeksName = findViewById<TextView>(R.id.item_weeks_name)
        val itemWeeksDiscountedPrice = findViewById<TextView>(R.id.item_weeks_discounted_price)
        val itemIndicatorRg = findViewById<RadioGroup>(R.id.item_indicator_rg)
        OkHttpManager.instance.requestInterface(object :OkHttpFromBoy{
            override fun addBody(requestBody: OkHttpBodyEntity) {
                requestBody.setPost(API.user_member_product_category_url)
                requestBody.add("productCategory",1)
            }

        },object : SDOkHttpResoutCallBack<MemberSubscribeEntity>() {
            override fun onSuccess(entity: MemberSubscribeEntity) {
                val subscriptions = entity.data.subscriptions
                if (subscriptions.size != itemIndicatorRg.childCount) {
                    itemIndicatorRg.removeViews(
                        subscriptions.size,
                        itemIndicatorRg.childCount - subscriptions.size
                    )
                }
                val pageBuyMemberAdapter = PageBuyMemberAdapter(ctx, subscriptions)
                dialogCycleBanner.adapter=pageBuyMemberAdapter
                dialogCycleBanner.currentItem=currentItem+1
                if (itemIndicatorRg.getChildAt(currentItem) != null){
                    (itemIndicatorRg.getChildAt(currentItem) as RadioButton).isChecked = true
                }
                dialogPurchaseNow.setOnClickListener {
                    var bean: MemberSubscribeEntity.Data.ProductDescriptions? = null
                    for(i in 0 until entity.data.productDescriptions.size){
                        if (entity.data.productDescriptions[i].check){
                            bean = entity.data.productDescriptions[i]
                            break
                        }
                    }
                    bean?.let { it1 -> listener.onListener(it1) }
                    dismiss()
                }

                val bean = entity.data
                quarterContainer.alpha=if (bean.productDescriptions[2].check) 1.0f else 0.6f
                quarterContainer.setBackgroundResource(if (bean.productDescriptions[2].check) R.drawable.shape_stroke_black_solid_yellow_radius_12 else R.drawable.shape_stroke_pale_yellow_radius_12)
                itemQuarterTitle.setBackgroundResource(if (bean.productDescriptions[2].check) R.drawable.shape_half_top_solid_black_radius_6 else R.drawable.shape_half_top_solid_pale_yellow_radius_6)
                itemQuarterTitle.text=bean.productDescriptions[2].tip
                itemQuarterNumber.text= bean.productDescriptions[2].benefitNum
                itemQuarterName.text=bean.productDescriptions[2].benefitUnit
                itemQuarterDiscountedPrice.text= bean.productDescriptions[2].price
                itemQuarterOriginalPrice.text="${bean.productDescriptions[2].priceOriginal}"
                itemQuarterOriginalPrice.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
                itemQuarterDiscount.text=bean.productDescriptions[2].saving

                monthlyContainer.alpha=if (bean.productDescriptions[1].check) 1.0f else 0.6f
                monthlyContainer.setBackgroundResource(if (bean.productDescriptions[1].check) R.drawable.shape_stroke_black_solid_yellow_radius_12 else R.drawable.shape_stroke_pale_yellow_radius_12)
                itemMonthlyTitle.setBackgroundResource(if (bean.productDescriptions[1].check) R.drawable.shape_half_top_solid_black_radius_6 else R.drawable.shape_half_top_solid_pale_yellow_radius_6)
                itemMonthlyTitle.text=bean.productDescriptions[1].tip
                itemMonthlyNumber.text= bean.productDescriptions[1].benefitNum
                itemMonthlyName.text=bean.productDescriptions[1].benefitUnit
                itemMonthlyDiscountedPrice.text= bean.productDescriptions[1].price
                itemMonthlyOriginalPrice.text="${bean.productDescriptions[1].priceOriginal}"
                itemMonthlyOriginalPrice.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
                itemMonthlyDiscount.text=bean.productDescriptions[1].saving

                weeksContainer.alpha=if (bean.productDescriptions[0].check) 1.0f else 0.6f
                weeksContainer.setBackgroundResource(if (bean.productDescriptions[0].check) R.drawable.shape_stroke_black_solid_yellow_radius_12 else R.drawable.shape_stroke_pale_yellow_radius_12)
                itemWeeksNumber.text= bean.productDescriptions[0].benefitNum
                itemWeeksName.text=bean.productDescriptions[0].benefitUnit
                itemWeeksDiscountedPrice.text= bean.productDescriptions[0].price



                quarterContainer.setOnClickListener {
                    quarterContainer.alpha=1.0f
                    quarterContainer.setBackgroundResource(R.drawable.shape_stroke_black_solid_yellow_radius_12)
                    itemQuarterTitle.setBackgroundResource(R.drawable.shape_half_top_solid_black_radius_6)
                    bean.productDescriptions[2].check=true


                    monthlyContainer.alpha=0.6f
                    monthlyContainer.setBackgroundResource(R.drawable.shape_stroke_pale_yellow_radius_12)
                    itemMonthlyTitle.setBackgroundResource(R.drawable.shape_half_top_solid_pale_yellow_radius_6)
                    bean.productDescriptions[1].check=false

                    weeksContainer.alpha=0.6f
                    weeksContainer.setBackgroundResource(R.drawable.shape_stroke_pale_yellow_radius_12)
                    bean.productDescriptions[0].check=false

                }

                monthlyContainer.setOnClickListener {
                    quarterContainer.alpha=0.6f
                    quarterContainer.setBackgroundResource(R.drawable.shape_stroke_pale_yellow_radius_12)
                    itemQuarterTitle.setBackgroundResource(R.drawable.shape_half_top_solid_pale_yellow_radius_6)
                    bean.productDescriptions[2].check=false

                    monthlyContainer.alpha=1.0f
                    monthlyContainer.setBackgroundResource(R.drawable.shape_stroke_black_solid_yellow_radius_12)
                    itemMonthlyTitle.setBackgroundResource(R.drawable.shape_half_top_solid_black_radius_6)
                    bean.productDescriptions[1].check=true

                    weeksContainer.alpha=0.6f
                    weeksContainer.setBackgroundResource(R.drawable.shape_stroke_pale_yellow_radius_12)
                    bean.productDescriptions[0].check=false

                }

                weeksContainer.setOnClickListener {
                    quarterContainer.alpha=0.6f
                    quarterContainer.setBackgroundResource(R.drawable.shape_stroke_pale_yellow_radius_12)
                    itemQuarterTitle.setBackgroundResource(R.drawable.shape_half_top_solid_pale_yellow_radius_6)
                    bean.productDescriptions[2].check=false

                    monthlyContainer.alpha=0.6f
                    monthlyContainer.setBackgroundResource(R.drawable.shape_stroke_pale_yellow_radius_12)
                    itemMonthlyTitle.setBackgroundResource(R.drawable.shape_half_top_solid_pale_yellow_radius_6)
                    bean.productDescriptions[1].check=false

                    weeksContainer.alpha=1f
                    weeksContainer.setBackgroundResource(R.drawable.shape_stroke_black_solid_yellow_radius_12)
                    bean.productDescriptions[0].check=true

                }
                dialogCycleBanner.addOnPageChangeListener(object : OnPageChangeListener {
                    override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {

                    }

                    override fun onPageSelected(p0: Int) {
                        Log.e("~~~", "onPageSelected: $p0 -- $currentItem" )
                        for (i in 0 until itemIndicatorRg.childCount){
                            (itemIndicatorRg.getChildAt(i) as RadioButton).isChecked = p0 == i
                        }
                    }

                    override fun onPageScrollStateChanged(p0: Int) {

                    }

                })

                showPopupWindow()
            }

        })

        dialogCancel.setOnClickListener {
            dismiss()
        }

        setOutSideDismiss(true)
    }

    interface ChangeMembershipListener{
        fun onListener(bean: MemberSubscribeEntity.Data.ProductDescriptions)
    }

}